// LineChartItem.h

#pragma once
#include <QQuickItem>
#include <QSGGeometryNode>
#include <QSGFlatColorMaterial>
#include <QVector>
#include <QPointF>

class LineChartItem : public QQuickItem {
    Q_OBJECT
    Q_PROPERTY(qreal scaleX READ scaleX WRITE setScaleX NOTIFY scaleChanged)
    Q_PROPERTY(qreal scaleY READ scaleY WRITE setScaleY NOTIFY scaleChanged)
    Q_PROPERTY(qreal offsetX READ offsetX WRITE setOffsetX NOTIFY offsetChanged)
    Q_PROPERTY(qreal offsetY READ offsetY WRITE setOffsetY NOTIFY offsetChanged)

public:
    LineChartItem(QQuickItem* parent = nullptr);

    // 单条折线接口
    Q_INVOKABLE void setData(const QVector<QPointF>& points);

    // 多条折线接口
    Q_INVOKABLE void setSeries(const QVector<QVector<QPointF>>& series);

    qreal scaleX() const { return m_scaleX; }
    qreal scaleY() const { return m_scaleY; }
    qreal offsetX() const { return m_offsetX; }
    qreal offsetY() const { return m_offsetY; }

    void setScaleX(qreal v) { m_scaleX = v; update(); emit scaleChanged(); }
    void setScaleY(qreal v) { m_scaleY = v; update(); emit scaleChanged(); }
    void setOffsetX(qreal v) { m_offsetX = v; update(); emit offsetChanged(); }
    void setOffsetY(qreal v) { m_offsetY = v; update(); emit offsetChanged(); }

signals:
    void scaleChanged();
    void offsetChanged();

protected:
    QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*) override;
    void wheelEvent(QWheelEvent* ev) override;
    void mousePressEvent(QMouseEvent* ev) override;
    void mouseMoveEvent(QMouseEvent* ev) override;

private:
    QVector<QVector<QPointF>> m_series;  // 支持多条折线

    qreal m_scaleX = 1.0;
    qreal m_scaleY = 1.0;
    qreal m_offsetX = 0.0;
    qreal m_offsetY = 0.0;

    QPointF m_lastMousePos;

    QSGNode* createGridNode();
    QSGNode* createAxisNode();
};
